package com.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotel.annotation.ControllerEndpoint;
import com.hotel.entity.Carousel;
import com.hotel.response.Result;
import com.hotel.service.CarouselService;
import com.hotel.vo.CarouselVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * 广告信息 前端控制器
 * </p>
 *
 * @author NieChangan
 * @since 2022-05-23
 */
@RestController
@Api(tags = "轮播图")
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @ApiOperation("轮播图上传")
    @ControllerEndpoint(exceptionMessage = "上传轮播图失败", operation = "上传轮播图")
    @PostMapping("/uploadCarousel")
    public Result uploadIcon(MultipartFile file){
        String s = carouselService.upload(file,"carousel_image");
        return Result.ok().data("url",s);
    }

    @GetMapping("/getCarouselList")
    @ApiOperation(value = "分页获取轮播图")
    public Result getCarouselList(@RequestParam(value = "carouselTitle",required = false) String carouselTitle,
                                  @RequestParam(value = "status",required = false)Integer status,
                                  @RequestParam(value = "current",required = false,defaultValue = "1")Integer current,
                                  @RequestParam(value = "size",required = false,defaultValue = "4")Integer size){
        IPage<Carousel> carouselList = carouselService.getCarouselList(carouselTitle, status, current, size);
        return Result.ok().data("carouselList",carouselList);
    }

    @DeleteMapping("/deleteCarouselById")
    @RequiresPermissions({"Shuffling:delete"})
    @ApiOperation(value = "删除轮播图")
    @ControllerEndpoint(exceptionMessage = "删除轮播图失败",operation = "删除轮播图信息")
    public Result deleteCarouselById(Integer id){
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();
        carouselQueryWrapper.eq("id",id);
        boolean remove = carouselService.remove(carouselQueryWrapper);
        return Result.ok().data("remove",remove);
    }

    @PutMapping("/updateStatus")
    @RequiresPermissions({"Shuffling:status"})
    @ControllerEndpoint(exceptionMessage = "更新轮播图状态失败", operation = "更新轮播图状态")
    @ApiOperation(value = "修改轮播图状态")
    public Result updateStatus(Integer id,Integer status){
        int i = carouselService.updateStatus(id, status);
        return Result.ok().data("updateStatus",i);
    }

    @PostMapping("/addCarousel")
    @RequiresPermissions({"Shuffling:add"})
    @ApiOperation(value = "增加轮播图")
    @ControllerEndpoint(exceptionMessage = "增加轮播图失败",operation = "增加轮播图信息")
    public Result addCarousel(@RequestBody CarouselVO carouselVO){
        int i = carouselService.addCarousel(carouselVO);
        return Result.ok().data("add",i);
    }

    @GetMapping("/editCarousel")
    @ApiOperation(value = "编辑轮播图")
    public Result editCarousel(Integer id){
        Carousel carousel = carouselService.editCarousel(id);
        return Result.ok().data("carousel",carousel);
    }

    @PostMapping("/updateCarousel")
    @RequiresPermissions({"Shuffling:update"})
    @ApiOperation(value = "修改轮播图")
    @ControllerEndpoint(exceptionMessage = "修改轮播图失败",operation = "修改轮播图信息")
    public Result updateCarousel(@RequestBody CarouselVO carouselVO){
        int i = carouselService.updateCarousel(carouselVO);
        return Result.ok().data("updateCarousel",i);
    }



}


